package org.netEase.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.netEase.dao.shopCardDao;
import org.netEase.dao.shoptypeDao;
import org.netEase.dao.impl.shopCardDaoImpl;
import org.netEase.dao.impl.shoptypeDaoImple;
import org.netEase.entity.goodsType;
import org.netEase.entity.shoppingCard;

import com.alibaba.fastjson.JSON;

@WebServlet(urlPatterns = { "/addCart.shtml" })
public class addcart extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String sid = request.getParameter("sid");
		String num = request.getParameter("num");
		String carttypeg = request.getParameter("carttypeg");
		String hid = request.getParameter("hid");
		String cart_img = request.getParameter("cart_img");
		int userid = 0;
		shopCardDao shopCardDao = new shopCardDaoImpl();
		shoptypeDao shoptypeDao = new shoptypeDaoImple();
		if (hid != "" && hid != null) {
			userid = Integer.parseInt(hid);
		}
		if (sid!=""&& sid!=null) {
			goodsType goodsType = shoptypeDao.listGetBySid(Integer.parseInt(sid));
			shoppingCard shoppingCard = new shoppingCard();
			shoppingCard.setCartname(goodsType.getShangping().getShopname());
			shoppingCard.setCart_typeg(carttypeg);
			shoppingCard.setCartimg(cart_img);
			shoppingCard.setCartnum(Integer.parseInt(num));
			shoppingCard.setCartprice(goodsType.getShangping().getSjiage());
			double totalPrice = Integer.parseInt(num) * goodsType.getShangping().getSjiage();
			shoppingCard.setCarttotal(totalPrice);
			shoppingCard.setHid(0);
			shoppingCard.setCart_spid(Integer.parseInt(sid));
			
			shopCardDao.addshopCard(shoppingCard);
		}
		List<shoppingCard> shoppingCard2 = shopCardDao.getlist(userid);
		response.setContentType("application/json;charset=utf-8");
		String shoppingCard3 = JSON.toJSONString(shoppingCard2);
		response.getWriter().write(shoppingCard3);

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}
